package com.ddd.mill.infrastructure.utils;

import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

/**
 * 分页参数接收
 *
 * @author zhoukunhua
 * @date 2019-10-16
 */
@Data
@ApiModel
public class PageUtil {
    @ApiModelProperty("页码")
    private Integer pageNum;
    @ApiModelProperty("页大小")
    private Integer pageSize;
    @ApiModelProperty("正序")
    private String asc;
    @ApiModelProperty("倒序")
    private String desc;

    public <T> Page<T> toPage(){
        Page<T> page = new Page<T>();
        if (this.pageNum != null){
            page.setCurrent(this.pageNum);
        }
        if (this.pageSize != null) {
            page.setSize(this.pageSize);
        }
        if (StringUtils.isNotEmpty(this.asc)) {
            String asc = StringUtils.underscoreName(this.asc);
            String[] ascArr = asc.split(",");
            page.setOrders(OrderItem.ascs(ascArr));
        }
        if (StringUtils.isNotEmpty(this.desc)) {
            String desc = StringUtils.underscoreName(this.desc);
            String[] descArr = desc.split(",");
            page.setOrders(OrderItem.descs(descArr));
        }
        if (StringUtils.isEmpty(this.asc) && StringUtils.isEmpty(this.desc)){
            page.setOrders(OrderItem.descs("create_time"));
        }
        return page;
    }
}
